/* Start Header *****************************************************************/
/*!
\file    Timer.cpp
\author  Li YeWEi
\par     y.li\@digipen.edu
\date    September 28, 2011
\brief
		this function update current time and get delata time of game.
	
Copyright (C) 2011 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior 
written consent of DigiPen Institute of Technology is prohibited.
*/
/* End Header *******************************************************************/
#include "PreCompiledHeader.h"
#include "Timer.h"



/********************************************************************************/
/*!
\brief
default constructor
*/
/********************************************************************************/
Timer::Timer():lockFrameRate(false),/*delta_time(0),*/frequency(0), cFPSLockRate( 1/240.0f),delta_time(0.0f)
{
	QueryPerformanceCounter(&currTime);
}

/********************************************************************************/
/*!
\brief
	get singlton 

*/
/********************************************************************************/
Timer* Timer::GetInstance()
{
	static Timer timerClass;
	return &timerClass;
}

/********************************************************************************/
/*!
\brief
	Update current time with new machine time
*/
/********************************************************************************/
void Timer::UpdateCurrTime()
{
	currTime = newTime;
}

/********************************************************************************/
/*!
\brief
	Get the new time.
*/
/********************************************************************************/
void Timer::UpdateNewTime()
{
	::QueryPerformanceCounter(&newTime);
}

/********************************************************************************/
/*!
\brief
	Grab the frequency of the computer's proccessor. 
*/
/********************************************************************************/
void Timer::UpdateFrequency()
{
	LARGE_INTEGER a;
	::QueryPerformanceFrequency(&a);
	frequency = a.QuadPart;
}

/********************************************************************************/
/*!
\brief
	Update the delta time
*/
/********************************************************************************/
void Timer::UpdateDeltaTime()
{
	UpdateFrequency();
	delta_time = static_cast<float>(newTime.QuadPart - currTime.QuadPart) / frequency;
	
	//delta_time = delta_time > 0.1f ? 0.1f: delta_time;  ---> lock the fps to 100


	//delta_time = delta_time > 1/60 ? 1/60 : delta_time;
	//delta_time = ( delta_time > FRAME_RATE_MIN )? FRAME_RATE_MIN : delta_time;
}

/********************************************************************************/
/*!
\brief
Giving the delta_time to the caller
\return delta_time
the delta time
*/
/********************************************************************************/
float Timer::GetDeltaTime()
{
  return delta_time;//( delta_time > FRAME_RATE_MIN )? FRAME_RATE_MIN : delta_time;
}


void Timer::SetFPSLockRate( float fpslRate )
{
  this->cFPSLockRate = fpslRate;
}

float Timer::GetFPS()
{
  return static_cast<float>(1.00/delta_time);
}

/********************************************************************************/
/*!
\brief
	This function will lock the framerate to what we set. but not be used in 
	our game.
*/
/********************************************************************************/
void Timer::FrameRateLocker()
{
  	do
  	{
		//UpdateCurrTime();
  		UpdateNewTime();
  		UpdateDeltaTime();
  	}while(delta_time < this->cFPSLockRate);
}
